import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2024-02-02
 * Time: 18:01
 */
public class test {
    public static void main(String[] args) throws InterruptedException, IOException {
        //test01();
        //test02();
        //test05();
        //test06();
        //浏览器操作
        //test07();
        //键盘操作
        //test08();
        //鼠标操作
        //test09();
        //定位一组数据
        //test10();

        //层级定位
        //test11();

        //下拉框定位
        //test12();

        //alert 链接的弹窗可以输入信息,输入后打印在网页上
        //test13()

        //上传
        //page05();
        //浏览器关闭
        //test14();
        //切换页面
        //test15();
        
        //截图
        test16();


    }

    private static void test16() throws InterruptedException, IOException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);

        //截图
        File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("D://2024jietu.png"));

    }

    private static void test15() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        sleep(3000);
        //找到新闻图标并点击
        webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).click();
        sleep(3000);

        //getWindowHandles获取所有的窗口句柄
        //getWindowHandle获取get打开的页面窗口句柄
        Set<String> handles = webDriver.getWindowHandles();
        String target_handle= "";
        for (String handle:handles) {  //遍历
            target_handle = handle;  //赋值
        }
        webDriver.switchTo().window(target_handle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        //点击百度一下
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
    }

    private static void test14() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        sleep(3000);
        //找到新闻图标并点击
        webDriver.findElement(By.xpath("//*[@id=\"s-top-left\"]/a[1]")).click();
        //webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();  两种方法
        webDriver.quit();  //整个浏览器关闭
        webDriver.close(); // 关闭当前页面

    }

    private static void page05() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/J20240202/%E6%B5%8B%E8%AF%95/J20240202/src/main/page/%E4%B8%8A%E4%BC%A0.html?_ijt=kh2amvkh324jnfhkpe4bn6t6t1&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("input")).sendKeys("D:\\东软作业\\数据结构实验");

    }
    private static void test13() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //链接是不对的
        webDriver.get("http://localhost:63342/J20240202/%E6%B5%8B%E8%AF%95/J20240202/src/main/page/test03.html?_ijt=v6vfksdpm41u9di3ur40cvfjul&_ij_reload=RELOAD_ON_SAVE");
        webDriver.findElement(By.cssSelector("#button")).click(); //弹窗是button实现的
        //强制等待3秒
        sleep(3000);
        //alert弹窗取消
        webDriver.switchTo().alert().dismiss();
        //强制等待3秒
        sleep(3000);
        //点击按钮
        webDriver.findElement(By.cssSelector("#button")).click(); //弹窗是button实现的
        //在alert弹窗中输入沙天其
        webDriver.switchTo().alert().sendKeys("沙天其");
        //强制等待3秒
        sleep(3000);
        //alert弹窗确认
        webDriver.switchTo().alert().accept();

    }

    private static void test12() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/J20240202/%E6%B5%8B%E8%AF%95/J20240202/src/main/page/test03.html?_ijt=v6vfksdpm41u9di3ur40cvfjul&_ij_reload=RELOAD_ON_SAVE");
        WebElement element = webDriver.findElement(By.cssSelector("#ShippingMethod"));
        Select select = new Select(element);
        //通过下拉框的索引找到
        select.selectByIndex(3);
        //通过元素的value值找到
        select.selectByValue("12.51");
    }

    private static void test11() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/J20240202/%E6%B5%8B%E8%AF%95/J20240202/src/main/page/test02.html?_ijt=cj69ecujl092m5b2q4fabd35ng&_ij_reload=RELOAD_ON_SAVE");
        webDriver.switchTo().frame("f1");   //最顶层那个frame的id
        webDriver.findElement(By.cssSelector("body > div > div > a")).click();
    }

    private static void test10() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://localhost:63342/J20240202/%E6%B5%8B%E8%AF%95/J20240202/src/main/page/test01.html?_ijt=ggou1c2u8ieegfhcion28o8m5k&_ij_reload=RELOAD_ON_SAVE");
        //隐式等待
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.DAYS);
        List<WebElement> webElements = webDriver.findElements(By.cssSelector("input"));
        for (int i = 0; i < webElements.size(); i++) {
            //元素type的值等于checkbox就进行点击
            if (webElements.get(i).getAttribute("type").equals("checkbox")) {  //getAttribute获取页面上元素的属性值,type是属性
                webElements.get(i).click();
            }
        }
    }

    private static void test09() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //搜索520
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("520");//找到搜索框输入"520"
        webDriver.findElement(By.cssSelector("#su")).click();  //点击搜索
        //强制等待3秒
        sleep(3000);
        //找到图片按钮
        WebElement webElement = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-pic_p4Uej.s-tab-pic"));
        //强制等待3秒
        sleep(3000);
        //鼠标右击
        Actions action = new Actions(webDriver);
        action.moveToElement(webElement).contextClick().perform();  //移动到图片按钮右键 (必须perform执行)
    }

    private static void test08() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        //ctrl+A
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"A");
        //强制等待3秒
        sleep(3000);
        //ctrl+X
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"X");
        //强制等待3秒
        sleep(3000);
        //ctrl+V
        webDriver.findElement(By.cssSelector("#kw")).sendKeys(Keys.CONTROL,"V");

    }

    private static void test07() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //搜索521
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("521");
        webDriver.findElement(By.cssSelector("#su")).click();  //点击
        //强制等待3秒
        sleep(3000);
        //浏览器后退
        webDriver.navigate().back();
        //强制等待3秒
        sleep(3000);
        //刷新
        webDriver.navigate().refresh();
        //浏览器前进
        webDriver.navigate().forward();
        //强制等待3秒
        sleep(3000);
        //滚动条来到最下面
        ((JavascriptExecutor)webDriver).executeScript("document.documentElement.scrollTop=10000");
        //浏览器最大化
        webDriver.manage().window().maximize();
        //强制等待3秒
        sleep(3000);
        //全屏
        webDriver.manage().window().fullscreen();
        //强制等待3秒
        sleep(3000);
        webDriver.manage().window().setSize(new Dimension(600,1000));
    }

    private static void test06() {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com/");
        //判断元素是否可以被点击
        WebDriverWait wait = new WebDriverWait(webDriver, 3000);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#bottom_layer > div > p:nth-child(8) > span")));
    }

    private static void test05() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");

        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();

        //校验
        if(url.equals("https://www.baidu.com/") && title.equals("百度一下，你就知道")) {
            System.out.println("当前页面url: "+url+".当前页面title: "+title);
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
    }

    private static void test02() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        sleep(3000);
        //找到百度搜索输入框 并输入Java
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("java");
        //点击百度一下按钮  //点击.click与submit的功能一样
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);

        //清空输入框的数据
        webDriver.findElement(By.cssSelector("#kw")).clear();
    }

    private static void test01() throws InterruptedException {
        int flag = 0;

        WebDriver webDriver = new ChromeDriver();
        //打开百度首页
        webDriver.get("https://www.baidu.com");
        //找到百度搜索框
        //WebElement element = webDriver.findElement(By.className("s_ipt"));  //css选择器定位元素
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));  //xpath方式
        //输入软件测试
        element.sendKeys("软件测试");
        //找到百度一下按钮
        //点击.click
        webDriver.findElement(By.cssSelector("#su")).click();

        //隐式等待3天
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        

        sleep(3000);
        //校验
        //找到搜索结果
        List<WebElement> elements = webDriver.findElements(By.cssSelector("a em"));
        for (int i = 0; i < elements.size(); i++) {

            //System.out.println(elements.get(i).getText());
            //如果返回的结果有软件测试,证明测试通过
            if (elements.get(i).getText().equals("软件测试")) {
                flag = 1;
                System.out.println("测试通过");
                break;
            }
            if (flag == 0) {
                System.out.println("测试不通过");
            }
        }

    }
}
